<?php

use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use app\modules\Basic\models\HotelBrand;
use app\modules\Basic\models\CityCode;
use kartik\select2\Select2;

//获取酒店品牌
$brand = HotelBrand::find()->all();
$brands = [];

foreach($brand as $br){
    $brands[$br->id] = $br->en_name.'——'.$br->zh_name;
}  

//获取城市
$city = CityCode::find()->all();
$citys = [];
foreach($city as $ct){
    $citys[$ct->code] = $ct->code.'——'.$ct->en_name.'——'.$ct->zh_name;
}  


?>

<div class="supplier-form">

    <?php $form = ActiveForm::begin([
                 'id' => 'dynamic-form',
                 'options' => ['enctype' => 'multipart/form-data',],
                 'layout' => 'horizontal',
                 'enableAjaxValidation'=>true,
                 'fieldConfig' => [
                    'template' => "{label}\n{beginWrapper}\n{input}\n{hint}\n{error}\n{endWrapper}",
                    'horizontalCssClasses' => [
                        'label' => 'col-sm-3',
                        'wrapper' => 'col-sm-8',
                        'error' => '',
                    ],
                 ],
            ]); ?>

    <?= $form->field($model, 'type')->inline(true)->radioList(Yii::$app->params['supplier_type']) ?>

    <?= $form->field($model, 'en_name')->textInput(['maxlength' => true]) ?>

    <div id="hotel_id" style="<?=$model->type == 1 ?'display: block' :'display: none'?>" >
    <?= $form->field($model, 'hotel_group')->widget(Select2::classname(), [
                'data' => $brands,
                'language' => 'zh-cn',
                'options' => ['placeholder' => '酒店品牌'],
                'pluginOptions' => [
                    'allowClear' => true,
                ],
            ]); ?>
    </div>

    <div id="phone_id" style="<?=$model->type == 5 ? 'display:none' :'display: block'?>" >

    <?= $form->field($model, 'person')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'address')->textArea(['maxlength' => true]) ?>

    </div>
    <?= $form->field($model, 'phone')->widget(\yii\widgets\MaskedInput::className(), [
        'mask' => '(999) 999-9999',
    ]) ?>

    <?= $form->field($model, 'city')->widget(Select2::classname(), [
                'data' => $citys,
                'language' => 'zh-cn',
                'options' => ['placeholder' => '选择城市'],
                'pluginOptions' => [
                    'allowClear' => true,
                ],
            ]); ?>

    <?= $form->field($model, 'email')->textInput(['maxlength' => true]) ?>
    
    <?= $form->field($model, 'url')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'remark')->textArea(['maxlength' => true]) ?>


    <div class="form-group text-center">
        <?= Html::submitButton(Yii::t('app', 'Save'), ['class' => 'btn btn-success']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>


<?php app\components\JsBlock::begin() ?>
<script type="text/javascript">  
    $(function(){ 
        $("#dynamic-form").on("change","#supplier-type",function(){
            var value = $("input[name='Supplier[type]']:checked").val();
            if(value == 1){
                $("#hotel_id").show();
                $("#phone_id").show();
            }else if(value == 5){
                $("#hotel_id").hide();
                $("#phone_id").hide();
            }else{
                $("#hotel_id").hide();
                $("#phone_id").show();
            }
        });    
     });
</script>
<?php app\components\JsBlock::end() ?>